//
//  MineFeedBackViewViewController.m
//  02-徙游记
//
//  Created by fanzhuxian on 2017/7/16.
//  Copyright © 2017年 FanZhuxian. All rights reserved.
//

#import "MineFeedBackViewViewController.h"
#import "SZKImagePickerVC.h"

@interface MineFeedBackViewViewController ()<SZKImagePickerVCDelegate, UITextViewDelegate> {

    NSMutableArray * imagesArray;
    
    NSMutableDictionary * imageDic;
}

@property (nonatomic, copy) NSString * stringType;

@property (nonatomic, strong) UITapGestureRecognizer * ges;

@end

@implementation MineFeedBackViewViewController

#pragma mark - init
- (instancetype)init {

    self = [super init];
    
    if (self) {
        imagesArray = [[NSMutableArray alloc] init];
        imageDic = [[NSMutableDictionary alloc] init];
    }
    
    return self;
}

#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.frame = CGRectMake(0, 60, DEVICE_SCREEN_WIDTH, DEVICE_SCREEN_HEIGHT);
    self.view.backgroundColor = RGB(235, 235, 241);
    self.title = @"建议";
    
    self.view.userInteractionEnabled = YES;
    self.ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideTextView)];
    
    // 初始化视图
    [self setSubViews];
}

- (void)hideTextView {

    [_suggestTextView resignFirstResponder];
}

#pragma mark - 初始化视图
- (void)setSubViews {

    _suggestTextView.layer.cornerRadius = 4;
    _suggestTextView.layer.masksToBounds = YES;
    _suggestTextView.text = @"请提出你的意见与建议";
    [_suggestTextView setTextColor:RGB(214, 226, 238)];
    _suggestTextView.backgroundColor = [UIColor whiteColor];
    _suggestTextView.delegate = self;
    
    _addImgView.layer.cornerRadius = 3;
    _addImgView.layer.masksToBounds = YES;
    _addImgView.userInteractionEnabled = YES;
    _addImgView.backgroundColor = [UIColor whiteColor];
    
    UITapGestureRecognizer * addGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addImg)];
    [_addImgView addGestureRecognizer:addGes];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(sendSuggest)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
}

#pragma mark - 点击事件
// 显示分享view
- (void)sendSuggest {

    [self alter];
}
    
#pragma mark - alter提示方法
- (void)alter {

    UIAlertController * alter = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"您的意见已提交，我们会尽快做出回复"] preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction * sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alter addAction:sureAction];
    [self.navigationController presentViewController:alter animated:YES completion:nil];
}

#pragma mark - textViewDelegate
- (void)textViewDidEndEditing:(UITextView *)textView {

    [self.view removeGestureRecognizer:self.ges];
    
    if (_suggestTextView.text.length > 0) {
        
    } else {
    
        [_suggestTextView setTextColor:RGB(235, 235, 241)];
        _suggestTextView.text = @"请提出您的意见与建议";
    }
}

- (void)textViewDidBeginEditing:(UITextView *)textView {

    [self.view addGestureRecognizer:self.ges];
    _suggestTextView.text = nil;
    
    [_suggestTextView setTextColor:[UIColor blackColor]];
}

#pragma mark - 添加图片点击事件
- (void)addImg {

    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction * camera = [UIAlertAction actionWithTitle:@"打开相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            [self presentViewController:ImagePickerStyleCamera];
        
        } else {
        
            NSLog(@"该设备不支持相机功能");
        }
    }];

    UIAlertAction * photo = [UIAlertAction actionWithTitle:@"打开相册" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
        [self presentViewController:ImagePickerStylePhotoLibrary];
    }];
    
    UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alert addAction:camera];
    [alert addAction:photo];
    [alert addAction:cancel];
    
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)presentViewController:(imagePickerStyle)style {

    SZKImagePickerVC * picker = [[SZKImagePickerVC alloc] initWithImagePickerStyle:style];
    picker.SZKDelegate = self;
    
    [self presentViewController:picker animated:YES completion:^{
        
    }];
}

#pragma mark - SZKImagePickerVCDelegate
- (void)imageChooseFinish:(UIImage *)image {

    [_addImgView setImage:image];
    
    // 保存到沙盒汇总
    [SZKImagePickerVC saveImageToSandbox:image andImageNage:@"addImage" andResultBlock:^(BOOL success) {
        NSLog(@"保存成功");
        
        [imagesArray addObject:image];
    }];
    
    [self addImageF];
}

#pragma mark - 图片处理，压缩图片
- (void)addImageF {

    _addImgView.image = imagesArray[0];
    NSData * imgData = UIImagePNGRepresentation(_addImgView.image); ///< 压缩体积
    
    if (imgData.length > 1024000) { ///< 图片不大于1M
        CGSize imgSize = CGSizeMake(_addImgView.image.size.width / 2, _addImgView.image.size.height / 2);
        _addImgView.image = [self OriginImage:_addImgView.image scaleToSize:imgSize];   ///< 压缩图片尺寸
    }
    
    [imageDic setObject:_addImgView.image forKey:[NSString stringWithFormat:@"imageData"]]; ///< 后台让你穿的图片对应的key
    
}

// 图片处理，图片压缩
- (UIImage *)OriginImage:(UIImage *)image scaleToSize:(CGSize)size {

    UIGraphicsBeginImageContext(size);    ///< 你所需要的图片尺寸
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage * scaleImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsPopContext();
    
    return scaleImage;  ///< 返回已变图片
}

- (IBAction)selectedType:(UIButton *)sender {
    
    if (sender.tag == 1) {
        [_projectBtn setImage:[UIImage imageNamed:@"feedback_selected"] forState:UIControlStateNormal];
        [_ableBtn setImage:[UIImage imageNamed:@"feedback_unSelected"] forState:UIControlStateNormal];
        
        _stringType = @"程序错误";
    
    } else if(sender.tag == 2) {
    
        [_ableBtn setImage:[UIImage imageNamed:@"feedback_selected"] forState:UIControlStateNormal];
        [_projectBtn setImage:[UIImage imageNamed:@"feedback_unSelected"] forState:UIControlStateNormal];
        
        _stringType = @"功能建议";
    }
}

@end
